package View.Repair;

import Service.RepairService;
import Service.ServiceImpl.RepairServiceImpl;
import View.Index.Index_admin;
import domain.Repair;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;

public class QueryRepair extends JFrame {


    public QueryRepair(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("查找报修记录");
        this.setSize(1050,518);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user) {
        //------------------JLabel--------------------
        JLabel label1 = new JLabel();
        label1.setText("id:");
        label1.setBounds(40, 18, 65, 30);
        JLabel label2 = new JLabel();
        label2.setText("宿舍号:");
        label2.setBounds(222, 18, 65, 30);
        JLabel label3 = new JLabel();
        label3.setText("问题:");
        label3.setBounds(422, 19, 65, 30);
        JLabel label4 = new JLabel();
        label4.setText("描述:");
        label4.setBounds(623, 20, 65, 30);
        JLabel label5 = new JLabel();
        label5.setText("是否完成:");
        label5.setBounds(830, 20, 65, 30);

        //------------------JTextField--------------------
        JTextField field1 = new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(90, 18, 120, 30);
        JTextField field2 = new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(293, 19, 120, 30);
        JTextField field3 = new JTextField();
        field3.setText("");
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setBounds(493, 19, 120, 30);
        JTextField field4 = new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(699, 20, 120, 30);
        JTextField field5 = new JTextField();
        field5.setText("");
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(890, 20, 120, 30);

        //------------------JButton--------------------
        JButton button1 = new JButton();
        button1.setText("查询");
        button1.setBounds(250, 67, 175, 43);
        JButton button2 = new JButton();
        button2.setText("退出");
        button2.setBounds(550, 67, 175, 43);

        JScrollPane panel = new JScrollPane();
        panel.setBounds(120, 120, 750, 350);


        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(label5);
        this.add(field1);
        this.add(field2);
        this.add(field3);
        this.add(field4);
        this.add(field5);
        this.add(button1);
        this.add(button2);
        this.add(panel);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String title[] = {"id","宿舍号","问题","描述","是否已进行维修"};
                if(!"".equals(field1) && !"".equals(field2) && !"".equals(field3) && !"".equals(field4) && !"".equals(field5)){
                    System.out.println("联合模糊查询触发触发");
                    HashMap<String,String> map = new HashMap<>();
                    map.put("id",field1.getText());
                    map.put("dormnum",field2.getText());
                    map.put("problem",field3.getText());
                    map.put("desc",field4.getText());
                    map.put("isfinish",field5.getText());

                    RepairService service = new RepairServiceImpl();
                    List<Repair> repairs = service.fuzzySelectRepqir(map);

                    Object[][] neirong = new Object[repairs.size()][5];
                    for (int i = 0; i < repairs.size(); i++) {
                        Repair repair = repairs.get(i);
                        neirong[i][0] = repair.getId();
                        neirong[i][1] = repair.getDormnum();
                        neirong[i][2] = repair.getProblem();
                        neirong[i][3] = repair.getDescription();
                        neirong[i][4] =repair.getIsfinish();
                    }
                    JTable table = new JTable(neirong, title);
                    table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                    DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
                    tcr.setHorizontalAlignment(SwingConstants.CENTER);
                    table.setDefaultRenderer(Object.class, tcr);
                    panel.setViewportView(table);
                }else {
                    RepairService service = new RepairServiceImpl();
                    List<Repair> repairs = service.repairList();

                    Object[][] neirong = new Object[repairs.size()][5];
                    for (int i = 0; i < repairs.size(); i++) {
                        Repair repair = repairs.get(i);
                        neirong[i][0] = repair.getId();
                        neirong[i][1] = repair.getDormnum();
                        neirong[i][2] = repair.getProblem();
                        neirong[i][3] = repair.getDescription();
                        neirong[i][4] =repair.getIsfinish();
                    }
                    JTable table = new JTable(neirong, title);
                    table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                    DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
                    tcr.setHorizontalAlignment(SwingConstants.CENTER);
                    table.setDefaultRenderer(Object.class, tcr);
                    panel.setViewportView(table);
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });
    }

}
